home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / datatypes / font_dt / src / otag.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  5KB  |  144 lines

  1. /*
  2. **    otag.c - .otag files reading routines
  3. **    Copyright © 1995 Michael Letowski
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  8. #include <diskfont/diskfont.h>
  9. #include <diskfont/diskfonttag.h>
  10. #include <graphics/text.h>
  11. #include <support/types.h>
  12.  
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <proto/diskfont.h>
  16. #include <proto/utility.h>
  17.  
  18. #include "classbase.h"
  19. #include "otag.h"
  20.  
  21. #define FTSUFFIX                ".font"
  22.  
  23. #define SUFFIX_LEN            5
  24. #define MAX_NAME_LEN        32
  25.  
  26. /* Get size of FontContentHeader */
  27. #define SizeOfFCH(fch)    (sizeof(struct FontContentsHeader)+\
  28.                                                             (fch)->fch_NumEntries*sizeof(struct FontContents))
  29. #define VarSizeOfFCH(x)    (sizeof(struct FontContentsHeader)+\
  30.                                                             (x)*sizeof(struct FontContents))
  31.  
  32. INLINE STATIC struct FontContentsHeader *ReadOutlines(struct ClassBase *cb,
  33.                                                                                                             BPTR lock, STRPTR name);
  34. STATIC LONG LocSeekFileSize(struct ClassBase *cb, BPTR fh);
  35.  
  36. /****************************************************************************/
  37. /*    These functions are necessary, because Diskfont doesn't work well     */
  38. /****************************************************************************/
  39.  
  40. struct FontContentsHeader *NewFC(struct ClassBase *cb, BPTR lock, STRPTR name)
  41. {
  42.     struct FontContentsHeader *FCH,*MyFCH=NULL;
  43.     ULONG Size;
  44.  
  45.     if(FCH=NewFontContents(lock,name))                        /* Use Diskfont to get contents */
  46.     {
  47.         if(FCH->fch_NumEntries)                                            /* Some entries exist */
  48.         {
  49.             Size=SizeOfFCH(FCH);
  50.             if(MyFCH=AllocVec(Size,MEMF_CLEAR))                /* Allocate new space */
  51.                 CopyMem(FCH,MyFCH,Size);                                /* Make a copy */
  52.         }
  53.         else if(FCH->fch_FileID==OFCH_ID)                        /* This is outline font */
  54.         {
  55.             if(MyFCH=ReadOutlines(cb,lock,name))            /* Read tags and sizes */
  56.                 MyFCH->fch_FileID=FCH->fch_FileID;
  57.         }
  58.         DisposeFontContents(FCH);                                        /* Free unused header */
  59.     }
  60.     return(MyFCH);
  61. }    /* NewFC */
  62.  
  63. VOID DisposeFC(struct ClassBase *cb, struct FontContentsHeader *fch)
  64. {
  65.     FreeVec(fch);
  66. }    /* DisposeFC */
  67.  
  68. INLINE STATIC struct FontContentsHeader *ReadOutlines(struct ClassBase *cb,
  69.                                                                                                             BPTR lock, STRPTR name)
  70. {
  71.     struct FontContentsHeader *FCH=NULL;
  72.     struct TFontContents *TFC;
  73.     struct TagItem *Tags;
  74.     struct FileInfoBlock *FIB;
  75.     UWORD *Sizes;
  76.     CHAR NewName[MAX_NAME_LEN];
  77.     STRPTR Suffix;
  78.     BPTR FH,OldLock;
  79.     ULONG Offset,I;
  80.     LONG FileLen;
  81.  
  82.     OldLock=CurrentDir(lock);                                            /* Change to FONTS: dir */
  83.  
  84.     /* Chage extension */
  85.     clear(&NewName);                                                            /* Set to NULLs */
  86.     strncpy(NewName,name,MAX_NAME_LEN-1);                    /* Copy name to buffer */
  87.     unless(Suffix=strstr(NewName,FTSUFFIX))
  88.         throw2(SetIoErr(ERROR_OBJECT_WRONG_TYPE),    EXIT);
  89.     strncpy(Suffix,OTSUFFIX,SUFFIX_LEN);                    /* Replace ".font" with ".otag" */
  90.  
  91.     /* Open .otag file and get its size */
  92.     try(FH=Open(NewName,MODE_OLDFILE),                        NO_FILE);
  93.     if(FIB=AllocDosObject(DOS_FIB,NULL))
  94.     {
  95.         if(ExamineFH(FH,FIB))                                                /* Examine it */
  96.             FileLen=FIB->fib_Size;                                        /* Get length of file */
  97.         else
  98.             FileLen=LocSeekFileSize(cb,FH);                        /* Get size by seeking */
  99.         FreeDosObject(DOS_FIB,FIB);
  100.     }
  101.     else
  102.         FileLen=LocSeekFileSize(cb,FH);                                /* No FIB? - seek */
  103.  
  104.     try(FileLen>=0,    ERROR1);                                            /* Make sure size is positive */
  105.     try(Tags=AllocVec(FileLen,MEMF_ANY),                    NO_TAGS);    /* Allocate space for tags */
  106.     try(Read(FH,Tags,FileLen)==FileLen,                        ERROR2);    /* Read tags */
  107.     try(GetTagData(OT_FileIdent,0,Tags)==FileLen,    ERROR2);    /* Verify size */
  108.  
  109.     /* Get size of new FCH and set up data */
  110.     try(Offset=GetTagData(OT_AvailSizes,0,Tags),    ERROR2);
  111.     Sizes=(UWORD *)((ULONG)Tags+Offset);                    /* Calculate position of data */
  112.     if(FCH=AllocVec(VarSizeOfFCH(*Sizes),MEMF_CLEAR))
  113.     {
  114.         FCH->fch_NumEntries=*Sizes;
  115.         for(I=0; I<*Sizes; I++)
  116.         {
  117.             TFC=&TFontContents(FCH)[I];
  118.             SNPrintf(TFC->tfc_FileName,MAXFONTPATH,"%s/%ld",name,Sizes[I+1]);
  119.             TFC->tfc_TagCount=1;                                            /* TAG_DONE */
  120.             TFC->tfc_YSize=Sizes[I+1];
  121.             TFC->tfc_Style=FS_NORMAL;                                    /* Really ??? */
  122.             TFC->tfc_Flags=FPF_DISKFONT;                            /* Really ??? */
  123.         }
  124.     }
  125.  
  126.     except(ERROR2,    );
  127.     except(NO_TAGS,    FreeVec(Tags));
  128.     except(ERROR1,    );
  129.     except(NO_FILE,    Close(FH));                                        /* Close opened .otag file */
  130.     except(EXIT,        );
  131.  
  132.     CurrentDir(OldLock);                                                    /* Get back to old dir */
  133.     return(FCH);
  134. }    /* ReadOutlines */
  135.  
  136. STATIC LONG LocSeekFileSize(struct ClassBase *cb, BPTR fh)
  137. {
  138.     LONG Size;
  139.  
  140.     if((Size=Seek(fh,0,OFFSET_END))>=0)                        /* Seek to end of file */
  141.         Size=Seek(fh,0,OFFSET_BEGINNING);                        /* Seek back to beginning */
  142.     return(Size);
  143. }    /* LocSeekFileSize */
  144.